#include "Link.h"

#include <QPainter>
#include <QRegion>
#include <QGraphicsScene>

//---------------------------------------------------------
Link::Link(QDeclarativeItem *parent) :
    QDeclarativeItem(parent),
    mA(0), mB(0), mAPoint(0),
    mColor(0, 0, 0), mPenWidth(1)
{
    //qDebug() << "Link(QDeclarativeItem *parent)";
    init();
}

//---------------------------------------------------------
Link::Link(const Link &other) :
    QDeclarativeItem((QDeclarativeItem*)other.parent()),
    mA(other.a()), mB(other.b()), mAPoint(other.mAPoint),
    mColor(other.mColor), mPenWidth(other.mPenWidth)
{
    //qDebug() << "Link(const Link &other)";
    init();
}

//---------------------------------------------------------
Link::Link(QDeclarativeItem &a, QDeclarativeItem &b, int aPoint) :
    QDeclarativeItem(&b),
    mA(&a), mB(&b), mAPoint(aPoint),
    mColor(0, 0, 0), mPenWidth(1)
{
    init();
}

//---------------------------------------------------------
QDeclarativeItem* Link::a() const { return mA; }
void Link::setA(QDeclarativeItem *a)
{
    if ( mA != a )
    {
        disconnect(mA, SIGNAL(destroyed()), this, SLOT(aDestroyed()));
        mA = a;
        connect(mA, SIGNAL(destroyed()), SLOT(aDestroyed()));
        emit aChanged();
    }
}

QDeclarativeItem* Link::b() const { return mB; }
void Link::setB(QDeclarativeItem *b)
{
    if ( mB != b )
    {
        disconnect(mB, SIGNAL(destroyed()), this, SLOT(bDestroyed()));
        mB = b;
        connect(mB, SIGNAL(destroyed()), SLOT(bDestroyed()));
        emit bChanged();
    }
}

//---------------------------------------------------------
void Link::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    if ( mA && mB )
    {
        QPen pen(mColor, mPenWidth);
        painter->setPen(pen);

        if ( smooth() )
            painter->setRenderHint(QPainter::Antialiasing, true);

        QString ptname = mA->objectName() + QString("_point%1").arg(mAPoint);
        QDeclarativeItem *aConn = mA->findChild<QDeclarativeItem*>(ptname);
        QDeclarativeItem *bConn = *(QDeclarativeItem**) mB->property("root").constData();

        QRectF arect(aConn->x() + mA->x() - mB->x(), aConn->y() + mA->y() - mB->y(), aConn->width(), aConn->height());
        QRectF brect(bConn->x(), bConn->y(), bConn->width(), bConn->height());

        QLineF line(arect.center(), brect.center());

        painter->drawLine(line);

        if ( widget )
            widget->update();
   }
}

//---------------------------------------------------------
void Link::redraw()
{
    // TODO: code here
}

//---------------------------------------------------------
void Link::init()
{
    setFlag(QGraphicsItem::ItemHasNoContents, false);
}

//---------------------------------------------------------
QColor Link::color() const { return mColor; }
void Link::setColor(QColor color)
{
    if ( color != mColor )
    {
        mColor = color;
        emit colorChanged();
    }
}

//---------------------------------------------------------
int Link::lineWidth() const { return mPenWidth; }
void Link::setLineWidth(int lineWidth)
{
    if ( lineWidth != mPenWidth )
    {
        mPenWidth = lineWidth;
        emit lineWidthChanged();
    }
}

//---------------------------------------------------------
int Link::aPoint() const { return mAPoint; }
void Link::setAPoint(int aPoint)
{
    if ( aPoint != mAPoint )
    {
        mAPoint = aPoint;
        emit aPointChanged();
    }
}

//---------------------------------------------------------
void Link::aDestroyed() { mA = 0; }
void Link::bDestroyed() { mB = 0; }

//---------------------------------------------------------
/*Link::~Link()
{
    delete mColor;
}*/
